package android.app.gfestival.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.gfestival.R;
import android.app.gfestival.R.id;
import android.app.gfestival.R.layout;
import android.app.gfestival.R.style;
import android.app.gfestival.adapter.ProgramFistAdapter;
import android.app.gfestival.adapter.ProgramSecondAdapter;
import android.app.gfestival.soap.Event;
import android.app.gfestival.soap.EventDate;
import android.app.gfestival.soap.SoapParserHelper;
import android.app.gfestival.utils.DateTimeUtil;
import android.content.Context;
import android.db.service.DatabaseService;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class ProgramViewSwicher extends ViewSwitcher{

	private LayoutInflater inflater;
	private LinearLayout secondScreen;
	private String lastData = "";
	private boolean isSecondScreen;
	private int position;
	private List<EventDate> dayEvents;
	private Activity context;
	private ProgressDialog progress;
	private EventDate eventDate;
	private DatabaseService service;
	private ListView fistScreenListView;
	private LinearLayout fistScreen;

	public ProgramViewSwicher(Activity context, DatabaseService service) {
		super(context);
		position = 0;
		this.context = context;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		fistScreen = (LinearLayout) inflater.inflate(R.layout.program_fist_screen, null);
		fistScreenListView = (ListView) fistScreen.findViewById(R.id.listview_pro_fist);
		dayEvents = new ArrayList<EventDate>();
		this.service = service;
		
		fistScreenListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				showNext(dayEvents.get(position));
			}
		});
		this.setOutAnimation(context, android.R.anim.slide_out_right);
		this.setInAnimation(context, android.R.anim.slide_in_left);
		progress = showProgressDialog();
		new DownloadDataAsynTask().execute(null);
	}
	
	public int showPreviou(){
		position --;
		if(position < 0) return position;
		showPrevious();
		return position;
	}
	public void showNext(EventDate eventDate){
		this.eventDate = eventDate;
		progress = showProgressDialog();
		position ++;
		if(lastData.equals(eventDate.event_day_id)){
			showNext();
			progress.dismiss();
			return;
		}else{
			new ProgramAsynTask().execute(eventDate.event_day_id);
		}
		
//		if(secondScreen != null) this.removeView(secondScreen);
//		secondScreen = (LinearLayout) inflater.inflate(R.layout.program_second_screen, null);
//		ListView secondListView = (ListView) secondScreen.findViewById(R.id.listview_pro_second);
//		TextView tv = (TextView) secondScreen.findViewById(R.id.textview_pro_screen_2_title);
//		tv.setText(DateTimeUtil.convertDate(eventDate.event_day,"EEEE dd - MMM yyyy").replace("-", "of"));
//		List<Event> list = new ArrayList<Event>();
//		try {
//			list = SoapParserHelper.getAllEventById(eventDate.event_day_id);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		ArrayList<Artist> list = new ArrayList<Artist>();
//		Random r = new Random();
//		for(int i =0; i < 5; i++){
//			Artist art = new Artist();
//			art.name = "Blindboys of Alabama (US)";
//			art.time = r.nextInt(24) + ":" + r.nextInt(60);
//			list.add(art);
//		}
//		ProgramSecondAdapter adapter = new ProgramSecondAdapter(context, list);
//		secondListView.setAdapter(adapter);
//		lastData = eventDate.event_day_id;
//		this.addView(secondScreen);
//		showNext();
//		progress.dismiss();
	}
	
	public ProgressDialog showProgressDialog() {
		ProgressDialog progress = new ProgressDialog(context.getParent(), R.style.ProgressDialogSpinner);
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setMessage("Loading...");
		progress.show();
		return progress;
	}
	
	private class DownloadDataAsynTask extends AsyncTask<String, String, List<EventDate>>{

		@Override
		protected List<EventDate> doInBackground(String... params) {
			try {
				if(service.isInternetConnected()){
					dayEvents = SoapParserHelper.getAllDayHaveEvents();
					service.getEventDateDAO().deleteAll();
					service.getEventDateDAO().insertList(dayEvents);
				}
				else
					dayEvents = service.getEventDateDAO().findAll();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return dayEvents;
		}
		
		@Override
		protected void onPostExecute(List<EventDate> result) {
			ProgramFistAdapter fistScreenAdapter = new ProgramFistAdapter(context, result);
			fistScreenListView.setAdapter(fistScreenAdapter);
			addView(fistScreen);
			progress.dismiss();
		}
		
	}
	
	private class ProgramAsynTask extends AsyncTask<String, String, List<Event>>{

		@Override
		protected List<Event> doInBackground(String... params) {
			
			List<Event> list = new ArrayList<Event>();
			try {
				if(service.isInternetConnected()){
					list = SoapParserHelper.getAllEventById(params[0]);
					Map<String, String> wheres = new HashMap<String, String>();
					wheres.put("event_day_id", params[0]);
					service.getEventDAO().delete(wheres);
					service.getEventDAO().insertList(list);
				}else{
					Map<String, String> wheres = new HashMap<String, String>();
					wheres.put("event_day_id", params[0]);
					list = service.getEventDAO().findByParam(wheres);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		}
		
		@Override
		protected void onPostExecute(List<Event> result) {
			// TODO Auto-generated method stub
			if(secondScreen != null) removeView(secondScreen);
			secondScreen = (LinearLayout) inflater.inflate(R.layout.program_second_screen, null);
			ListView secondListView = (ListView) secondScreen.findViewById(R.id.listview_pro_second);
			TextView tv = (TextView) secondScreen.findViewById(R.id.textview_pro_screen_2_title);
			String day = DateTimeUtil.convertDate(eventDate.event_day,"EEEE dd.MM.yy").replace("-", "of");
			
			tv.setText(DateTimeUtil.convertDay(day,context));
			ProgramSecondAdapter adapter = new ProgramSecondAdapter(context, result);
			secondListView.setAdapter(adapter);
			lastData = eventDate.event_day_id;
			addView(secondScreen);
			showNext();
			progress.dismiss();
		}
		
		
		
	}
	
}
